package com.apptude.android.trivial.gpswatch;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.location.GpsSatellite;
import android.location.GpsStatus;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class SignalStrengthView extends View
{

	public SignalStrengthView(Context context, AttributeSet attrs)
	{
		super(context, attrs);
	}

	public void gpsStatusChanged(GpsStatus gpsStatus)
	{
		myGpsStatus = gpsStatus;
		invalidate();
	}
	@Override
	protected void onDraw(Canvas canvas)
	{
		final int width = getWidth();
		final int height = getHeight();
		
		/* paint the background this will erase the whole visible (clipped) view */
		canvas.drawColor(Color.BLACK);
		
		if (null != myGpsStatus)
		{
			final int maxSatellites = myGpsStatus.getMaxSatellites();
			
			if (null == arraySatelliteSnr || arraySatelliteSnr.length < maxSatellites)
			{
				arraySatelliteSnr = new float[maxSatellites];
			}
			
			if (null == arraySatelliteUsedInFix || arraySatelliteUsedInFix.length < maxSatellites)
			{
				arraySatelliteUsedInFix = new boolean[maxSatellites];
			}
			
			for (int nIndex = 0; nIndex < maxSatellites; ++nIndex)
			{
				arraySatelliteSnr[nIndex] = -1.0f;				// less than NO signal
			}
			
			Iterable<GpsSatellite> satellites = myGpsStatus.getSatellites();
		
			for (GpsSatellite gpsSatellite : satellites)
			{
				final int prn = gpsSatellite.getPrn();
				final float snr = gpsSatellite.getSnr();
				
				arraySatelliteSnr[prn] = snr;
				arraySatelliteUsedInFix[prn] = gpsSatellite.usedInFix();
				
				if (snr > maxSnr)
				{
					maxSnr = snr;
				}
			}
			
			int baseX = 0 + 4;					// in from the left and ...
			final int baseY = height - 14;		// up from the bottom
			final int minY = 0 + 4;				// down from the top
			final float pixelsPerSnr = (baseY - minY) / maxSnr;
			
			for (int nIndex = 0; nIndex < maxSatellites; ++nIndex)
			{
				final float snr = arraySatelliteSnr[nIndex];
				if (snr >= 0.0f)
				{
//					canvas.drawText(String.format("%d", nIndex), baseX, baseY, textPaint);
	
					final Paint thisPaint = arraySatelliteUsedInFix[nIndex] ?  satelliteFixPaint : satelliteNoFixPaint;
					final float toY = baseY - snr *  pixelsPerSnr;
					canvas.drawLine(baseX,     baseY, baseX,     toY, thisPaint);
					canvas.drawLine(baseX + 1, baseY, baseX + 1, toY, thisPaint);
					canvas.drawLine(baseX + 2, baseY, baseX + 2, toY, thisPaint);
					canvas.drawLine(baseX + 3, baseY, baseX + 3, toY, thisPaint);
	
					baseX += 20;
				}
			}
		}
	}

	private GpsStatus	myGpsStatus;
	
	private float []	arraySatelliteSnr;
	private boolean []	arraySatelliteUsedInFix;
	private float		maxSnr = -1.0f;
	
	final static Paint satelliteFixPaint;
	final static Paint satelliteNoFixPaint;
	final static Paint textPaint;
	
	static
	{
		satelliteFixPaint = new Paint();
		satelliteFixPaint.setColor(Color.RED);
		satelliteFixPaint.setStyle(Paint.Style.STROKE);
		satelliteFixPaint.setAntiAlias(false);
		
		satelliteNoFixPaint = new Paint();
		satelliteNoFixPaint.setColor(Color.WHITE);					
		satelliteNoFixPaint.setStyle(Paint.Style.STROKE);		
		satelliteNoFixPaint.setAntiAlias(false);
		
		textPaint = new Paint();
		textPaint.setColor(Color.WHITE);
		textPaint.setTextSize(16.0f);
	}
}
